package com.jvup.common.widget.adapter.auto;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager.widget.PagerAdapter;

import com.jvup.common.basic.ObjectUtil;
import com.jvup.common.widget.adapter.auto.annotation.AdapterEntityBind;

import java.util.HashMap;
import java.util.List;

public class SummerPagerAdapter<T extends AutoVariable> extends PagerAdapter {

    private final HashMap<Integer, ViewHolder> viewHolderMap = new HashMap<>();

    /** ViewModel数据列表 */
    private final List<T> dataList;

    /** 事件处理器 */
    private AutoVariable eventProcess;

    public SummerPagerAdapter(List<T> dataList) {
        this(dataList, null);
    }

    public SummerPagerAdapter(List<T> dataList, AutoVariable eventProcess) {
        this.dataList = dataList;
        this.eventProcess = eventProcess;
    }

    /**
     * 设置事件处理器到所有项目View并进行绑定
     * @param eventProcess 事件处理器
     */
    public void setEventProcess(AutoVariable eventProcess) {
        this.eventProcess = eventProcess;
        notifyDataSetChanged();
    }

    /**
     * 通过ViewModel提取layoutRid，如果dataList为空，则无法提取会并会导致后续异常
     * @return layoutRid
     */
    private int fetchRootViewId(Class<?> dataClass) {
        if(dataClass == null || !dataClass.isAnnotationPresent(AdapterEntityBind.class)) {
            return 0;
        }
        AdapterEntityBind adapterEntityBind = dataClass.getAnnotation(AdapterEntityBind.class);
        return adapterEntityBind.viewRid();
    }

    private ViewHolder createHolder(ViewGroup container,int layoutId) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, container, false);
        return new ViewHolder(binding);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡

        ViewHolder viewHolder = viewHolderMap.get(position);
        AutoVariable data = dataList.get(position);
        if(ObjectUtil.isNull(viewHolder)) {
            viewHolder = createHolder(container, fetchRootViewId(data.getClass()));
            viewHolderMap.put(position, viewHolder);
        }

        viewHolder.bindData(data);
        viewHolder.bindData(eventProcess);

        container.addView(viewHolder.rootView(), 0);//添加页卡
        return viewHolder.rootView();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)   {
//        container.removeView(mListViews.get(position));//删除页卡
        if(ObjectUtil.nonNull(object)) {
            container.removeView((View) object);
        }
    }

    @Override
    public int getCount() {
        return  dataList.size();//返回页卡的数量
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;//官方提示这样写
    }


    /**
     * 缓存binder的处理器，负责绑定数据
     */
    protected class ViewHolder {
        protected final ViewDataBinding viewDataBinding;

        public ViewHolder(ViewDataBinding viewDataBinding) {
            this.viewDataBinding = viewDataBinding;
        }

        public View rootView() {
            return viewDataBinding.getRoot();
        }

        public void bindData(AutoVariable observable) {
            if(observable != null) {
                observable.bindVariable(viewDataBinding);
            }
        }
    }

}
